package com.acctrue.zentao.ui;

import com.acctrue.zentao.dto.LoginDTO;
import com.acctrue.zentao.utils.ResponseResult;
import com.acctrue.zentao.utils.StringUtils;
import com.acctrue.zentao.utils.ZentaoUtils;
import com.acctrue.zentao.entity.ZentaoSettingEntity;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;

import javax.swing.*;

/**
 * @author: 孙炯炯
 * @since: 2023/8/18
 **/
public class SettingsUI implements Configurable {
    private JPanel rootPanel;
    private JTextField txtServerAddress;
    private JTextField txtUserName;
    private JPasswordField txtPassword;

    @Override
    public String getDisplayName() {
        return "禅道登录配置";
    }

    @Override
    public JComponent createComponent() {
        return rootPanel;
    }

    @Override
    public boolean isModified() {
        PropertiesComponent prop = PropertiesComponent.getInstance();
        String storedServerAddress = StringUtils.getString(prop.getValue(ZentaoSettingEntity.ZENTAO_SERVER_ADDRESS));
        String storedUserName = StringUtils.getString(prop.getValue(ZentaoSettingEntity.ZENTAO_USERNAME));
        String storedPassword = StringUtils.getString(prop.getValue(ZentaoSettingEntity.ZENTAO_PASSWORD));

        String uiAddress = StringUtils.getString(txtServerAddress.getText());
        String uiUserName = StringUtils.getString(txtUserName.getText());
        String uiPassword = StringUtils.getString(txtPassword.getPassword());

        boolean result = storedServerAddress.equals(uiAddress) && storedUserName.equals(uiUserName) && storedPassword.equals(uiPassword);

        return !result;
    }

    @Override
    public void apply() throws ConfigurationException {
        String server = StringUtils.getString(txtServerAddress.getText());
        String userName = StringUtils.getString(txtUserName.getText());
        String password = StringUtils.getString(txtPassword.getPassword());
        PropertiesComponent prop = PropertiesComponent.getInstance();
        if (StringUtils.isNotEmpty(server)) {
            loginCheck(server, userName, password);
            prop.setValue(ZentaoSettingEntity.ZENTAO_SUCCESS, Boolean.TRUE.toString());
        } else {
            prop.setValue(ZentaoSettingEntity.ZENTAO_SUCCESS, Boolean.FALSE.toString());
        }
        prop.setValue(ZentaoSettingEntity.ZENTAO_SERVER_ADDRESS, StringUtils.getString(txtServerAddress.getText()));
        prop.setValue(ZentaoSettingEntity.ZENTAO_USERNAME, StringUtils.getString(txtUserName.getText()));
        prop.setValue(ZentaoSettingEntity.ZENTAO_PASSWORD, StringUtils.getString(txtPassword.getPassword()));
    }

    private void loginCheck(String server, String userName, String password) {
        if (StringUtils.isEmpty(server)) {
            JOptionPane.showMessageDialog(null, "请设置禅道地址");
            throw new RuntimeException("请设置禅道地址");
        }
        if (StringUtils.isEmpty(userName)) {
            JOptionPane.showMessageDialog(null, "请设置用户名");
            throw new RuntimeException("请设置用户名");
        }
        if (StringUtils.isEmpty(password)) {
            JOptionPane.showMessageDialog(null, "请设置密码");
            throw new RuntimeException("请设置密码");
        }
        if (server.endsWith("/")) {
            server = server.substring(0, server.length() - 1);
            txtServerAddress.setText(server);
        }
        LoginDTO.Input input = new LoginDTO.Input();
        input.setServerAddress(server);
        input.setUserName(userName);
        input.setPassword(password);
        ResponseResult<LoginDTO.Output> loginResult = ZentaoUtils.login(input);
        if (!loginResult.getSuccess()) {
            JOptionPane.showMessageDialog(null, loginResult.getMessage());
            throw new RuntimeException("禅道登录失败，请检查");
        }else{
            JOptionPane.showMessageDialog(null, "登录成功");
        }
    }

    @Override
    public void reset() {
        PropertiesComponent prop = PropertiesComponent.getInstance();
        txtServerAddress.setText(prop.getValue(ZentaoSettingEntity.ZENTAO_SERVER_ADDRESS));
        txtUserName.setText(prop.getValue(ZentaoSettingEntity.ZENTAO_USERNAME));
        txtPassword.setText(prop.getValue(ZentaoSettingEntity.ZENTAO_PASSWORD));
    }
}
